<!-- 承运商管理 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @row-update="rowUpdate"
    >
      <template slot-scope="{ disabled, size }" slot="expireDateSearch">
        <el-switch :disabled="disabled" :size="size" v-model="query.expireDate">
        </el-switch>
      </template>
      <template slot-scope="{ disabled, size }" slot="mailboxForm">
        <div>
          <avue-array
            :disabled="disabled"
            :size="size"
            v-model="form.mailbox"
            placeholder="请输入邮箱"
          ></avue-array>
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="menu">
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="getDriverList(row)"
          >司机列表</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="getEscortList(row)"
          >押运员列表</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="getCarList(row)"
          >车辆列表</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="getTankList(row)"
          >坦克罐列表</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click.stop="grouping(row)"
          >分配组别</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-key"
          @click.stop="rowResetPassword(row)"
          >重置密码</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click.stop="rowEdit(row, index)"
          >编辑</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          v-if="row.approvalStatus == '已驳回'"
          @click.stop="viewReject(row)"
          >驳回原因</el-button
        >
      </template>
    </avue-crud>
    <driverList ref="driverList" />
    <carList ref="carList" />
    <escortList ref="escortList" />
    <tankList ref="tankList" />
    <grouping ref="grouping" @refresh="onLoad(page, query)" />
  </basic-container>
</template>

<script>
import { getList, update, resetPassword } from "@/api/basedata/carriers";
import driverList from "./components/driverList";
import carList from "./components/carList";
import escortList from "./components/escortList";
import tankList from "./components/tankList";
import grouping from "./components/grouping";
import { cardid, isEmail } from "@/util/validate";

export default {
  data() {
    var validateLegalPersonNo = (rule, value, callback) => {
      if (value) {
        if (!cardid(value)[0]) {
          callback();
        } else {
          callback(new Error(cardid(value)[1]));
        }
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (value && value.length > 0) {
        let num = value.filter((i) => i && !isEmail(i));
        let valueArr = value.filter((i) => i);
        let newArr = [...new Set(valueArr)];
        if (newArr.length !== valueArr.length) {
          callback(new Error("请勿输入重复邮箱"));
        }
        if (num.length > 0) {
          callback(new Error("请输入正确的邮箱"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          {
            label: "业务伙伴编码",
            prop: "tmId",
            editDisabled: true,
          },
          {
            label: "名称",
            prop: "name",
            editDisabled: true,
            search: true,
          },
          {
            label: "组别",
            prop: "carrierGroup",
            editDisabled: true,
          },
          {
            label: "区域（省份）",
            prop: "addressProvince",
            editDisabled: true,
          },
          {
            label: "城市",
            prop: "addressCity",
            editDisabled: true,
          },
          {
            label: "街道",
            prop: "adddressStreet",
            editDisabled: true,
          },
          {
            label: "电话",
            prop: "phone",
            search: true,
            editDisabled: true,
          },
          {
            label: "审核状态",
            prop: "approvalStatus",
            editDisplay: false,
          },
          {
            label: "创建时间",
            type: "datetime",
            prop: "createTime",
            search: true,
            searchRange: true,
            editDisplay: false,
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "即将到期",
            prop: "expireDate",
            search: true,
            searchRange: true,
            editDisplay: false,
            addDisplay: false,
            hide: true,
            showColumn: false,
            searchSpan: 3,
          },
          {
            label: "公司名称",
            prop: "cpmpanyName",
            hide: true,
            showColumn: false,
            rules: [
              {
                required: true,
                message: "请输入公司名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "邮箱",
            prop: "mailbox",
            formslot: true,
            hide: true,
            showColumn: false,
            rules: [{ validator: validateEmail, trigger: "blur" }],
          },
          {
            label: "法人姓名",
            prop: "legalPersonName",
            hide: true,
            showColumn: false,
          },
          {
            label: "法人身份证号",
            prop: "legalPersonNo",
            hide: true,
            showColumn: false,
            rules: [
              {
                validator: validateLegalPersonNo,
                trigger: "blur",
              },
            ],
          },
          {
            label: "道路运输证营运证号",
            prop: "certificateTransportNo",
            hide: true,
            showColumn: false,
            // rules: [
            //   {
            //     required: true,
            //     message: "请输入道路运输证营运证号",
            //     trigger: "blur",
            //   },
            // ],
          },
          {
            label: "营业执照有效期",
            type: "datetimerange",
            prop: "certificateOperationValidDate",
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "道路运输证校验有效期",
            type: "datetimerange",
            prop: "certificateTransportValidDate",
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "法人身份证正面照片",
            prop: "legalPersonPicFront",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传法人身份证正面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "法人身份证背面照片上传",
            prop: "legalPersonPicBack",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传法人身份证背面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "营业执照照片上传",
            prop: "certificateOperationPic",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传营业执照照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "道路运输证照片上传",
            prop: "certificateTransportPic",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            // rules: [
            //   {
            //     required: true,
            //     message: "请上传道路运输证照片",
            //     trigger: "change",
            //   },
            // ],
          },
        ],
      },
      data: [],
    };
  },
  components: {
    driverList,
    carList,
    escortList,
    tankList,
    grouping,
  },
  computed: {},
  watch: {
    "query.expireDate"(val) {
      if (val === false || val === true) {
        this.page.currentPage = 1;
        this.onLoad(this.page, this.query);
      }
    },
  },
  methods: {
    rowEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
      this.form = {
        ...row,
        certificateOperationValidDate: [
          row.certificateOperationValidDateStart || "",
          row.certificateOperationValidDateEnd || "",
        ],
        certificateTransportValidDate: [
          row.certificateTransportValidDateStart || "",
          row.certificateTransportValidDateEnd || "",
        ],
        mailbox: row.mailbox.split(","),
      };
    },
    getDriverList(row) {
      this.$refs.driverList.open(row.id);
    },
    getCarList(row) {
      this.$refs.carList.open(row.id);
    },
    getTankList(row) {
      this.$refs.tankList.open(row.id);
    },
    getEscortList(row) {
      this.$refs.escortList.open(row.id);
    },
    grouping(row) {
      this.$refs.grouping.open(row.id, row.carrierGroup);
    },
    rowResetPassword(row) {
      this.$confirm("是否确定重置密码?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          resetPassword({ parentId: row.parentId }).then(
            () => {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //查看驳回原因
    viewReject(row) {
      this.$alert(row.remarkReject, "驳回原因", {
        showCancelButton: true,
        showConfirmButton: false,
        cancelButtonText: "关闭",
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (this.query.expireDate) {
        this.query = {
          ...params,
          expireDate: this.query.expireDate,
        };
      } else {
        this.query = params;
      }
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.createTime) {
        query = {
          ...query,
          start: query.createTime ? query.createTime[0] : "",
          end: query.createTime ? query.createTime[1] : "",
        };
        delete query.createTime;
      }
      this.loading = true;
      getList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //编辑
    rowUpdate(row, index, done, loading) {
      let updateData = {
        ...row,
        certificateOperationValidDateStart: row.certificateOperationValidDate
          ? row.certificateOperationValidDate[0]
          : "",
        certificateOperationValidDateEnd: row.certificateOperationValidDate
          ? row.certificateOperationValidDate[1]
          : "",
        certificateTransportValidDateStart: row.certificateTransportValidDate
          ? row.certificateTransportValidDate[0]
          : "",
        certificateTransportValidDateEnd: row.certificateTransportValidDate
          ? row.certificateTransportValidDate[1]
          : "",
        mailbox: row.mailbox.filter((i) => i).join(","),
      };
      update(updateData).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
  },
};
</script>

<style></style>
